package com.jacobson.core.mvp

import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import com.jacobson.core.base.BaseFragment
import com.jacobson.core.framework.dialog.DialogHandler
import javax.inject.Inject

/**
 * ================================================
 * 基于MVP模式需继承该Fragment
 * @author Wucz on 2018/7/16 14:55
 * ================================================
 */
abstract class BaseMvpFragment<P : IPresenter> : BaseFragment(), IView {
    @Inject
    lateinit var presenter: P
    @Inject
    lateinit var dialogHandler: DialogHandler

    fun preInitData(savedInstanceState: Bundle?) {
        presenter.onStart()
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroy()
    }


    override fun visibleDialog(type: Int, args: Bundle?): Boolean {
        return dialogHandler.showDialog(activity!!, type, args)
    }

    override fun invisibleDialog(type: Int) {
        dialogHandler.hideDialog(type)
    }

    override fun startLoading(type: Int, extra: Any?) {

    }

    override fun stopLoading(type: Int, extra: Any?) {

    }

    override fun onError(type: Int, extra: Any?, code: Int, message: String): Boolean = false

    override fun reload() {
        presenter.onStart()
    }

    override fun tipToLogin() {
        logger.d("require Logged State, But Not!")
        Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show()
    }

    override fun fetchActivity(): Activity? = activity
}